export default class PriorityQueue {
    private items: any[] = [];
    // 将元素入队列
    public enqueue(element: any, priority?: 1 | 2 | 3): void {
      let obj = { element, priority: priority || 3 };
      if (this.items.length === 0 || priority === undefined) {
        this.items.push(obj);
      } else {
        let findIndex = this.items.findIndex((item, index) => {
          return item.priority > obj.priority;
        });
        if (findIndex) {
          this.items.unshift(obj);
        } else {
          this.items.splice(findIndex, 0, obj);
        }
      }
    }
    // 出队列
    public dequeue(): any {
      return this.items.shift();
    }
    // 查看队列排在最前面的元素
    public front(): any {
      return this.items[0];
    }
    // 判断队列是否为空
    public isEmpty(): any {
      return !this.items.length;
    }
    // 队列中元素的个数
    public size(): number {
      return this.items.length;
    }
    // toString 12 235 25
    public toString(): string {
      let arr: any[] = [];
      this.items.forEach((item) => {
        arr.push(item.element);
      });
      return arr.join(" ");
    }
  }
